package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //先判断有无 该item
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.selectByEntity(shoppingCart);
        //无 item 判断是setmeal OR dish
        if(list == null || list.size() == 0){
            Long dishId = shoppingCartDTO.getDishId();
            if(dishId != null){
                //菜品 select 菜品 再 insert 菜品
                Dish dish = dishMapper.selectById(dishId);
                shoppingCart.setDishId(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }
            else {
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setSetmealId(setmeal.getId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            shoppingCartMapper.insert(shoppingCart);
        }
        //有item number ++
        else{
            shoppingCart = list.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateById(shoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> list() {
        Long user_id = BaseContext.getCurrentId();
        List<ShoppingCart> list = shoppingCartMapper.selectById(user_id);
        return list;
    }

    @Override
    public void clean() {
        Long user_id = BaseContext.getCurrentId();
        shoppingCartMapper.cleanById(user_id);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //判断 num
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //select num
        Long number = shoppingCartMapper.selectNum(shoppingCart);
        if(number<=1){
            shoppingCartMapper.sub(shoppingCart);
        }
        else{
            shoppingCartMapper.subOneById(shoppingCart);
        }
    }
}
